<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/9/9
 * Time: 15:35
 */
if (!defined('ACCESS')) {
    exit('Access denied.');
}

class AdminOrder extends Base
{

    private static $table_name = 'jh_order_sn_record';
    // 查询字段
    private static $columns = array('id', 'order_sn', 'userid', 'shipping_method', 'pay_type', 'user_name', 'shipping_time', 'phone', 'addres', 'gender', 'email', 'note', 'status', 'createtime', 'modifytime');

    public static function getTableName()
    {
        return self::$table_name;
    }


    public static function add($function_data)
    {
        if (!$function_data || !is_array($function_data)) {
            return false;
        }
        $db = self::__instance();
        $id = $db->insert(self::getTableName(), $function_data);
        return $id;
    }

    public static function getById($id)
    {
        $condition = array('id' => $id);
        $db = self::__instance();
        $rt = $db->get(self::getTableName(), self::$columns, $condition);
        return $rt;
    }

    public static function getByOrderSn($sn)
    {
        $condition = array('order_sn' => $sn);
        $db = self::__instance();
        $rt = $db->get(self::getTableName(), self::$columns, $condition);
        return $rt;
    }

    public static function update($data, $oid)
    {
        if (!$data || !is_array($data)) {
            return false;
        }
        $condition = array('id' => $oid);
        $db = self::__instance();
        $id = $db->update(self::getTableName(), $data, $condition);
        return $id;
    }

    public static function updateBySn($sn, $data)
    {
        if (!$data || !is_array($data)) {
            return false;
        }
        $condition = array('order_sn' => $sn);
        $db = self::__instance();
        $id = $db->update(self::getTableName(), $data, $condition);
        return $id;
    }

    public static function getAllOrders($start = '', $page_size = '', $search = '')
    {
        $db = self::__instance();
        if ($search) {
            $con = $search;
        } else {
            $con = " 1=1 ";
        }
        $where = "WHERE $con ORDER BY createtime DESC LIMIT $start,$page_size";
        $result = $db->select(self::getTableName(), self::$columns, $where);
        return $result;
    }


    public static function delete($sn)
    {
        $condition = array('order_sn' => $sn);
        $db = self::__instance();
        $result = $db->delete(self::getTableName(), $condition);
        return $result;
    }


    public static function getCountBySearch($condition = '')
    {
        $db = self::__instance();
        $result = $db->count(self::getTableName(), $condition);
        return $result;
    }


    public static function getOrdersBySearch($start, $page_size, $where)
    {

        $db = self::__instance();

        $sql = "SELECT * FROM " . self::getTableName() . $where . " ORDER BY createtime DESC LIMIT  $start,$page_size";

        $list = $db->query($sql)->fetchAll();

        if ($list) {
            return $list;
        }
        return array();
    }

    public static function getOrdersBySearchAll($where)
    {
        $db = self::__instance();

        $sql = "SELECT * FROM " . self::getTableName() . $where . " ORDER BY createtime DESC;";

        $list = $db->query($sql)->fetchAll();

        if ($list) {
            return $list;
        }
        return array();
    }
}




